Repository navigation
zeroize: add portable bulk data path for zeroizing slices - #1602
Conversation
Adds a bulk data path to the internal `volatile_set` function, using 16-byte chunks for types whose size is 1, 2, 4, or 8 bytes. Remaining elements and other type sizes retain element-sized volatile writes. Mutable chunks are produced using a copy of `as_chunks_mut` from Rust 1.88, vendored to preserve MSRV 1.85, with a TODO to replace the copy with the standard library method when MSRV permits. The `Zeroize` impl for `[Z]` already requires `DefaultIsZeroes`, which has `Default` and `Copy` supertraits. This evaluates `Default` once, fills an array with copies of the returned value, then repeatedly uses `volatile_write` to copy that array into the destination chunks. These array-valued volatile writes can lower to LLVM's `llvm.memcpy` intrinsic with its volatile flag set, allowing LLVM to select wider loads and stores when supported by the target.
|
Benchmark results on my local M3 Max: |
|
Seems fine, but for a better picture I think it's worth to include benchmarks for non-volatile write loop followed by let buf: &mut [T] = ...;
for val in buf {
*val = Default::default();
}
optimization_barrier(buf); |
|
I pushed up a side-by-side benchmark of that approach (I moved the It seems like the Results: All that said, I'm going to merge this as it's a definite improvement over the previous performance. I think there's some potential refactoring which could make it a little cleaner, but that can be done as a followup. |
Followup to #1602 which migrates to safe slice-based fills for initialized values, bypassing the slice-pointer-slice round trip which was previously happening with the `volatile_set`-based API, which has subsequently been replaced with `volatile_zero` for the non-slice based use cases. This can potentially be DRYed out further with `zeroize_flat_type`, which I'd like to explore in a further followup.
Followup to #1602 which migrates to safe slice-based fills for initialized values, bypassing the slice-pointer-slice round trip which was previously happening with the `volatile_set`-based API, which has subsequently been replaced with `volatile_zero` for the non-slice based use cases. This can potentially be DRYed out further with `zeroize_flat_type`, which I'd like to explore in a further followup.
Followup to #1602 which migrates to safe slice-based fills for initialized values, bypassing the slice-pointer-slice round trip which was previously happening with the `volatile_set`-based API, which has subsequently been replaced with `volatile_zero` for the non-slice based use cases. This can potentially be DRYed out further with `zeroize_flat_type`, which I'd like to explore in a further followup.
Adds an optimized byte slice zeroing function which uses a regular `fill` followed by `optimization_barrier` on platforms with stable `asm!` and falls back on the bulk volatile write implementation added in #1602 on other platforms. This approach should maintain the previous guarantees the purely volatile write-based approach provided on all platforms, while also providing the performance of the `optimization_barrier`-based approach on platforms with `asm!`. This function is public but also able to subsume the previous role of the internal `volatile_zero` function added in #1603.
Adds an optimized byte slice zeroing function which uses a regular `fill` followed by `optimization_barrier` on platforms with stable `asm!` and falls back on the bulk volatile write implementation added in #1602 on other platforms. This approach should maintain the previous guarantees the purely volatile write-based approach provided on all platforms, while also providing the performance of the `optimization_barrier`-based approach on platforms with `asm!`. This function is public but also able to subsume the previous role of the internal `volatile_zero` function added in #1603.
Adds an optimized byte slice zeroing function which uses a regular `fill` followed by `optimization_barrier` on platforms with stable `asm!` and falls back on the bulk volatile write implementation added in #1602 on other platforms. This approach should maintain the previous guarantees the purely volatile write-based approach provided on all platforms, while also providing the performance of the `optimization_barrier`-based approach on platforms with `asm!`. This function is public but also able to subsume the previous role of the internal `volatile_zero` function added in #1603.
Adds an optimized byte slice zeroing function which uses a regular `fill` followed by `optimization_barrier` on platforms with stable `asm!` and falls back on the bulk volatile write implementation added in #1602 on other platforms. This approach should maintain the previous guarantees the purely volatile write-based approach provided on all platforms, while also providing the performance of the `optimization_barrier`-based approach on platforms with `asm!`. This function is public but also able to subsume the previous role of the internal `volatile_zero` function added in #1603.
|
Good stuff, this yielded up to 3% improvements in rustls benchmarks that don't exercise any crypto! |
Adds a bulk data path to the internal
volatile_setfunction, using 16-byte chunks for types whose size is 1, 2, 4, or 8 bytes. Remaining elements and other type sizes retain element-sized volatile writes.Mutable chunks are produced using a copy of
as_chunks_mutfrom Rust 1.88, vendored to preserve MSRV 1.85, with a TODO to replace the copy with the standard library method when MSRV permits.The
Zeroizeimpl for[Z]already requiresDefaultIsZeroes, which hasDefaultandCopysupertraits. This evaluatesDefaultonce, fills an array with copies of the returned value, then repeatedly usesvolatile_writeto copy that array into the destination chunks.These array-valued volatile writes can lower to LLVM's
llvm.memcpyintrinsic with its volatile flag set, allowing LLVM to select wider loads and stores when supported by the target.